package shopping.dao.impl;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import shopping.dao.OrderDao;
import shopping.domain.Order;
import shopping.domain.OrderItem;
import shopping.domain.vo.OrderItemVo;
import shopping.utils.DataSourceUtil;

import java.sql.SQLException;
import java.util.List;

public class OrderDaoImpl implements OrderDao {

    @Override
    public void saveOrder(Order o) {
        QueryRunner qr = new QueryRunner();
        String sql = "insert into orders values(?,?,?,?,null,null,null,?)";
        try {
            qr.update(DataSourceUtil.getConnection(), sql, o.getOid(), o.getOrdertime(), o.getTotal(), o.getState(), o.getUid()
            );
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }
    }

    @Override
    public void saveOrderItem(OrderItem oi) {
        QueryRunner qr = new QueryRunner();
        String sql = "insert into orderitem values(?,?,?,?)";
        try {
            qr.update(DataSourceUtil.getConnection(), sql, oi.getCount(), oi.getSubTotal(), oi.getPid(), oi.getOid()
            );
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }
    }

    /**
     * 根据订单号oid查询订单信息
     * @param oid
     * @return
     */
    @Override
    public Order findByOid(String oid) {
        QueryRunner qr = new QueryRunner(DataSourceUtil.getDataSource());
        String sql = "select * from orders where oid = ?";

        try {
            return qr.query(sql, new BeanHandler<>(Order.class), oid);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }
    }

    /**
     * 根据订单号oid查询订单项信息
     * @param oid
     * @return
     */
    @Override
    public List<OrderItemVo> findItemsVo(String oid) {
        QueryRunner qr = new QueryRunner(DataSourceUtil.getDataSource());
        String sql = "SELECT oi.count, oi.subtotal,oi.pid,p.pname,p.pimage,p.shop_price AS price FROM orderitem oi, product p WHERE oi.pid = p.pid AND oid = ?;";

        try {
            return qr.query(sql, new BeanListHandler<>(OrderItemVo.class), oid);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }
    }

    /**
     *分页查询我的订单详情
     */
    @Override
    public List<Order> findOrderByUid(String uid, int pageNumber, int pageSize) {
        QueryRunner qr = new QueryRunner(DataSourceUtil.getDataSource());
        String sql = "select * from orders where uid = ? LIMIT ?,?";

        try {
            return qr.query(sql, new BeanListHandler<>(Order.class), uid,(pageNumber - 1) * pageSize, pageSize);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }
    }

    /**
     *查询一共有多少个订单
     */
    @Override
    public int countByUid(String uid) {
        QueryRunner qr = new QueryRunner(DataSourceUtil.getDataSource());
        String sql = "select count(*) from orders where uid = ?";

        try {
            return ((Long)qr.query(sql, new ScalarHandler(), uid)).intValue();
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }
    }

    @Override
    public void updateSHR(Order order) {
        QueryRunner qr = new QueryRunner(DataSourceUtil.getDataSource());
        String sql = "update orders set name=?, address=?, telephone=? where oid=?";

        try {
            qr.update(sql,order.getName(), order.getAddress(), order.getTelephone(), order.getOid());
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }
    }

    @Override
    public void updateState(String oid, int state) {
        QueryRunner qr = new QueryRunner(DataSourceUtil.getDataSource());
        String sql = "update orders set state=? where oid=?";

        try {
            qr.update(sql,state, oid);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }
    }
}
